package com.sensetime.jv.service.impl;

import com.sensetime.jv.mapper.UrlPermissionsMapper;
import com.sensetime.jv.service.UrlPermissionService;
import com.sensetime.jv.util.RedisCache;
import com.sensetime.jv.vo.request.UrlRolePermissionRespVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UrlPermissionServiceImpl implements UrlPermissionService {


    @Value("${redis.cache.custom.permissionUrlLock}")
    private String permissionKey;

    @Resource
    private UrlPermissionsMapper urlPermissionsMapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public List<UrlRolePermissionRespVO> getAllPermissions() {
        List<UrlRolePermissionRespVO> all =  redisCache.getCacheObject(permissionKey);
        if (CollectionUtils.isEmpty(all)){
            all = urlPermissionsMapper.selects();
            if (!CollectionUtils.isEmpty(all)){
                redisCache.setCacheObject(permissionKey,all,10, TimeUnit.MINUTES);
            }
        }
        return all;
    }

    @Override
    public void removeAll() {
        redisCache.deleteObject(permissionKey);
    }
}
